﻿using Map.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Map.Controllers
{
    public class DataController : AdminLoginBaseController
    {

        public JsonResult GetAllPoints()
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Points
                                 select item).ToList();
                    return Json(query);
                }
            }
            catch (Exception e)
            {
                return Json(null);
            }
        }

        public JsonResult GetAllUser()
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Users
                                 select item).ToList();
                    return Json(query);
                }
            }
            catch (Exception e)
            {
                return Json(null);
            }
        }

        public LineModel GetPointsByIds(string ids)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return null;
            }

            string[] arrIds = ids.Split(',');
            List<int> intIds = new List<int>();
            foreach (var id in arrIds)
            {
                intIds.Add(Convert.ToInt32(id));
            }
            var model = new LineModel();
            model.Point = new List<Points>();
            for (var i = 0; i < intIds.Count; i++)
            {
                var cid = intIds[i];
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Points
                                 where item.Id == cid
                                 select item).FirstOrDefault();


                    model.PointNames += query.Name + "-";
                    model.PointPositions += query.Lng + "," + query.Lat + "-";
                    model.Point.Add(query);

                }
            }
            model.PointNames.TrimEnd('-');
            return model;
        }

        public JsonResult GetLines(string name = "")
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Lines
                                 where item.Name == name || string.IsNullOrEmpty(name)
                                 select item).ToList();

                    var result = new List<LineModel>();
                    if (query.Any())
                    {
                        foreach (var item in query)
                        {
                            var model = GetPointsByIds(item.PointIds);
                            result.Add(new LineModel
                            {
                                Id = item.Id,
                                Name = item.Name,
                                CreateTime = item.CreateTime,
                                Content = model.PointNames,
                                Color = item.Color,
                                PointIds = item.PointIds,
                                Remark = item.Remark,
                                PointPositions = model.PointPositions,
                                Point = model.Point

                            });
                        }
                    }
                    return Json(result);
                }

            }
            catch (Exception e)
            {
                return Json(null);
            }
        }

        public JsonResult GetLinesName()
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Lines
                                 select new
                                 {
                                     Name = item.Name
                                 }).ToList();

                    return Json(query);
                }

            }
            catch (Exception e)
            {
                return Json(null);
            }
        }

        public JsonResult GetPointByLineName(string name)
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Lines
                                 where item.Name == name
                                 select new
                                 {
                                     Ids = item.PointIds
                                 }).ToList();

                    var points = new Dictionary<int, string>();
                    foreach (var item in query)
                    {
                        var model = GetPointsByIds(item.Ids);
                        if (model != null && model.Point.Any())
                        {
                            foreach (var p in model.Point)
                            {
                                if (!points.ContainsKey(p.Id))
                                {
                                    points.Add(p.Id, p.Name);
                                }
                            }
                        }
                    }
                    return Json(points.ToList());
                }

            }
            catch (Exception e)
            {
                return Json(null);
            }
        }

        public JsonResult AddPoint(Points p)
        {
            if (p == null)
            {
                return Json(false);
            }
            try
            {
                using (var context = new MapEntities())
                {
                    if (p.Id > 0)
                    {
                        var model = context.Points.Where(x => x.Id == p.Id).FirstOrDefault();
                        if (model != null)
                        {
                            model.PointType = p.PointType;
                            model.Name = p.Name;
                            model.Lng = p.Lng;
                            model.Lat = p.Lat;
                            model.Remark = p.Remark;
                        }
                    }
                    else {
                        p.CreateTime = DateTime.Now;
                        context.Points.Add(p);
                    }

                    var isOk = context.SaveChanges() > 0;
                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        public JsonResult DeletePoint(int id)
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var model = context.Points.Where(x => x.Id == id).FirstOrDefault();
                    if (model != null)
                    {
                        context.Points.Remove(model);
                    }

                    var isOk = context.SaveChanges() > 0;
                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        public JsonResult AddLine(Lines l)
        {
            if (l == null)
            {
                return Json(false);
            }
            try
            {
                using (var context = new MapEntities())
                {

                    if (l.Id > 0)
                    {
                        var model = context.Lines.Where(x => x.Id == l.Id).FirstOrDefault();
                        if (model != null)
                        {
                            model.Name = l.Name;
                            model.PointIds = l.PointIds;
                            model.Color = l.Color;
                            model.Remark = l.Remark;
                        }
                    }
                    else {
                        l.CreateTime = DateTime.Now;
                        context.Lines.Add(l);
                    }

                    var isOk = context.SaveChanges() > 0;

                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        public JsonResult DeleteLine(int id)
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var model = context.Lines.Where(x => x.Id == id).FirstOrDefault();
                    if (model != null)
                    {
                        context.Lines.Remove(model);
                    }

                    var isOk = context.SaveChanges() > 0;
                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        public JsonResult AddUser(Users p)
        {
            if (p == null)
            {
                return Json(false);
            }
            try
            {
                using (var context = new MapEntities())
                {
                    if (p.Id > 0)
                    {
                        var model = context.Users.Where(x => x.Id == p.Id).FirstOrDefault();
                        if (model != null)
                        {
                            model.Account = p.Account;
                            model.NickName = p.NickName;
                            model.Password = p.Password;
                            model.Role = p.Role;
                        }
                    }
                    else {
                        p.CreateTime = DateTime.Now;
                        context.Users.Add(p);
                    }

                    var isOk = context.SaveChanges() > 0;
                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        public JsonResult DeleteUser(int id)
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var model = context.Users.Where(x => x.Id == id).FirstOrDefault();
                    if (model != null)
                    {
                        context.Users.Remove(model);
                    }

                    var isOk = context.SaveChanges() > 0;
                    return Json(isOk);
                }
            }
            catch (Exception e)
            {
                return Json(false);
            }
        }

        //dingwei
        public JsonResult GetDWlines(string lineName, int point)
        {
            try
            {
                using (var context = new MapEntities())
                {
                    var query = (from item in context.Lines
                                 where item.Name == lineName
                                 select new
                                 {
                                     Id = item.Id,
                                     Ids = item.PointIds
                                 }).ToList();

                    var result = new Dictionary<int, List<Points>>();
                    foreach (var item in query)
                    {
                        var model = GetPointsByIds(item.Ids);
                        for (var i = 0; i < model.Point.Count; i++)
                        {
                            if (model.Point[i].Id != point)
                            {
                                model.Point.RemoveAt(i);
                                i--;
                            }
                            else
                            {
                                result.Add(item.Id, model.Point);
                                break;
                            }
                        }
                    }

                    return Json(result.ToList());
                }
            }
            catch
            {
                return null;
            }
        }

    }
}